package com.ulic.customerManApplication.usermodule.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseAction;
import com.ulic.customerManApplication.commonmodule.bo.Activie;
import com.ulic.customerManApplication.commonmodule.bo.Menu;
import com.ulic.customerManApplication.commonmodule.service.ActivieService;
import com.ulic.customerManApplication.commonmodule.service.MenuService;
import com.ulic.customerManApplication.commonmodule.service.OrganizationService;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.usermodule.bo.Role;
import com.ulic.customerManApplication.usermodule.bo.User;
import com.ulic.customerManApplication.usermodule.service.PermissionService;
import com.ulic.customerManApplication.usermodule.service.RoleService;
import com.ulic.customerManApplication.usermodule.service.UserService;

@Component("userAction")
@Scope("prototype")
public class UserAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private UserService userServices;
	private MenuService menuService;
	private PermissionService permissionService;
	private RoleService roleService;
	private ActivieService activieService;
	private OrganizationService organizationService;
	private User user;
	private String oldPassword;
	private String password;
	private int[] pmenu;
	private int[] pfunction;
	private int[] puser;
	private String userid;
	private int roleId;
	private int activieOid;
	private String roletype;
	private String activietype;
	private String startDate;
	private String endDate;
	public String organOid;
	public String userList() throws Exception {
		try {
			HttpServletRequest httpServletRequest = ServletActionContext. getRequest();
			StringBuilder sb = new StringBuilder();
			if(organOid!=null && organOid.length()>0){
				String[] ids = organOid.split(":");
				for(String id : ids){
					sb.append("'"+id+"',");
				}
			}
			PagerModel pm = userServices.getPager(curPage, user, roletype, activietype, startDate, endDate,sb.toString());
			httpServletRequest.setAttribute("pm",pm);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return SUCCESS;
	}
	
	/**
	 * Description: 查看用户
	 * @Version1.0 
	 * 2012-7-11 下午04:54:01 李求智 (liqz@huilan.com) created
	 * @return
	 * @throws Exception
	 */
	public String viewUser() throws Exception{
		user = userServices.findUserByUserId(userid);
		roletype = getActivityName(userid);
		activietype = getRoleName(userid);
		return SUCCESS;
	}
	
	public String setPermission() throws Exception {
		//读取所有菜单
		List<Menu> menuList = menuService.getMenu();
		request.put("menuList", menuList);
		//读取有权限的菜单
		int[] pmenu = permissionService.getUserPermission(userid);
		request.put("pmenu", pmenu);
		List<Role> roleList = roleService.getRole();
		request.put("roleList", roleList);
		//读取已有状态
		int[] roles = roleService.getUserRole(userid);
		request.put("roles", roles);
		List<Activie> activieList = activieService.getActivie();
		request.put("activieList", activieList);
		//读取已有角色
		int[] activies = activieService.getUserActivie(userid);
		request.put("activies", activies);
		return SUCCESS;
	}
	
	public String setedPermission() throws Exception {
		permissionService.setUserPermission(userid,pmenu);
		roleService.setUserRole(userid, roleId);
		activieService.setUserActivie(userid, activieOid);
		return SUCCESS;
	}

	public String getRoleName(String userId)throws Exception{
		int[] roles = roleService.getUserRole(userId);
		if(roles!=null&&roles.length>0){
			return roleService.getRole(roles[0]).getName();
		}else{
			return "";
		}
	}
	
	public String getActivityName(String userId)throws Exception{
		int[] activies = activieService.getUserActivie(userId);
		if(activies!=null&&activies.length>0){
			return activieService.getActivie(activies[0]).getName();
		}else{
			return "";
		}
	}
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}
	
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
	public int[] getPmenu() {
		return pmenu;
	}

	public void setPmenu(int[] pmenu) {
		this.pmenu = pmenu;
	}

	public int[] getPfunction() {
		return pfunction;
	}

	public void setPfunction(int[] pfunction) {
		this.pfunction = pfunction;
	}

	public int[] getPuser() {
		return puser;
	}

	public void setPuser(int[] puser) {
		this.puser = puser;
	}

	@Resource
	public void setUserServices(UserService userServices) {
		this.userServices = userServices;
	}

	@Resource
	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}
	@Resource
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	@Resource
	public void setUserRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
	@Resource
	public void setActivieService(ActivieService activieService) {
		this.activieService = activieService;
	}

	public int getActivieOid() {
		return activieOid;
	}

	public void setActivieOid(int activieOid) {
		this.activieOid = activieOid;
	}

	public String getRoletype() {
		return roletype;
	}

	public void setRoletype(String roletype) {
		this.roletype = roletype;
	}

	public String getActivietype() {
		return activietype;
	}

	public void setActivietype(String activietype) {
		this.activietype = activietype;
	}

	public String getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getOrganOid() {
		return organOid;
	}

	public void setOrganOid(String organOid) {
		this.organOid = organOid;
	}

	@Resource
	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}
	
	
	
	
}
